package org.dolphin.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.dolphin.base.bean.CityVO;
import org.dolphin.base.bean.RegionVO;
import org.dolphin.dao.DistrictDao;
import org.dolphin.entity.district.City;
import org.dolphin.entity.district.Region;
import org.dolphin.service.DistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "districtService")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class DistrictServiceImpl implements DistrictService {
  @Autowired(required = true)
  private DistrictDao districtDao;

  @Override
  public List<CityVO> getCities(List<Long> regions) {
    List<City> cities = districtDao.getCities(regions);
    List<CityVO> results = Collections.emptyList();
    if (cities != null && !cities.isEmpty()) {
      results = new ArrayList<CityVO>(regions.size());
      for (City city : cities) {
        CityVO vo = new CityVO();
        vo.setId(city.getId());
        vo.setName(city.getName());
        results.add(vo);
      }
    }
    return results;
  }

  @Override
  public List<RegionVO> getRegions() {
    List<Region> regions = districtDao.getRegions();
    List<RegionVO> results = Collections.emptyList();
    if (regions != null && !regions.isEmpty()) {
      results = new ArrayList<RegionVO>(regions.size());
      for (Region region : regions) {
        RegionVO vo = new RegionVO();
        vo.setId(region.getId());
        vo.setName(region.getName());
        results.add(vo);
      }
    }
    return results;
  }

  @Override
  public City loadCity(String id) {
    return districtDao.loadCity(id);
  }
}
